﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

public partial class Setup_Columns : System.Web.UI.Page
{
    public string tabname = string.Empty;
    public string xmlname = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        string tableName = Request.QueryString["tab"];
        tabname = tableName;
        string xmlFileName = Request.QueryString["xml"];
        xmlname = xmlFileName;

        string xmlFilePath = Server.MapPath("~/configs/tables/" + tableName + "/" + xmlFileName + ".xml");

        if (!Page.IsPostBack)
        {
            ConfigGlobalSettings globalSettings = ConfigGlobal.GetGlobal();
            string roleSql = "select " + globalSettings.RoleTablePkFieldName + "," + globalSettings.RoleTableNameFieldName + " from " + globalSettings.RoleTableName;
            string adminSql = "select " + globalSettings.AdminPKColumnField + "," + globalSettings.UserNameColumnField + " from " + globalSettings.AdminTableName;

            if (globalSettings.AllowRole)
            {
                this.allowroles.DataValueField = globalSettings.RoleTablePkFieldName;
                this.allowroles.DataTextField = globalSettings.RoleTableNameFieldName;
                this.allowroles.DataSource = DBFactory.GetConn().exeTable(roleSql);
                this.allowroles.DataBind();
            }

            if (globalSettings.AllowBrowseControl)
            {
                this.allowusers.DataValueField = globalSettings.AdminPKColumnField;
                this.allowusers.DataTextField = globalSettings.UserNameColumnField;
                this.allowusers.DataSource = DBFactory.GetConn().exeTable(adminSql);
                this.allowusers.DataBind();
            }

            this.HyperLink1.NavigateUrl = "tablexml.aspx?tab="+tableName;
            ConfigTable cTab = xmlFilePath.XmlToObj<ConfigTable>();

            this.tableShowName.Text = cTab.TableShowName;
            this.locationText.Text = cTab.LocationText;

            this.beforeinsert.Text = cTab.BeforeInsertClassName;
            this.beforeupdate.Text = cTab.BeforeUpdateClassName;
            this.beforedelete.Text = cTab.BeforeDeleteClassName;

            this.afterinsert.Text = cTab.AfterInsertClassName;
            this.afterupdate.Text = cTab.AfterUpdateClassName;
            this.afterdelete.Text = cTab.AfterDeleteClassName;

            this.pname.Text = cTab.ParentColumnName;
            this.prootvalue.Text = cTab.ParentRootValue;
            this.depth.Text = cTab.ChildDepth.ToString();

            this.tablename.Text = cTab.TableName;
            this.allowadd.Checked = cTab.AllowAdd;
            this.allowedit.Checked = cTab.AllowEdit;
            this.allowdel.Checked = cTab.AllowDel;
            this.allowshow.Checked = cTab.AllowShow;
            this.issingle.Checked = cTab.SingleType;
            this.allowpageing.Checked = cTab.AllowPageing;
            this.pagesize.Text = cTab.PageSize.ToString();
            this.allowmselect.Checked = cTab.AllowMultiSelect;
            this.allowmove.Checked = cTab.AllowMove;
            this.typetabparamname.Text = cTab.TypeTableParamName;
            this.allowoutput.Checked = cTab.AllowOutput;

            this.pkfieldname.Text = cTab.PkFiledName;
            this.fkfieldname.Text = cTab.FkFiledName;
            this.fkmaintablename.Text = cTab.FkMainTableName;

            this.orderfieldname.Text = cTab.OrderFieldName;
            this.ordersql.Text = cTab.OrderSql;
            this.ordergroup.Text = cTab.OrderGroupColumnName;

            this.allowrolectrl.Checked = cTab.AllowRolebrowseContrl;
            this.allowuserctrl.Checked = cTab.AllowUserBrowseContrl;

            this.listurl.Text = cTab.ListUrl;
            this.listurlparam.Text = cTab.ListUrlParameters.ToSplitTrimString();
            this.viewname.Text = cTab.ListViewName;
            this.vxmlname.Text = cTab.ListViewXmlName;
            this.addurl.Text = cTab.AddUrl;
            this.addurlparam.Text = cTab.AddUrlParameters.ToSplitTrimString();
            this.editurl.Text = cTab.EditUrl;
            this.editurlparam.Text = cTab.EditUrlParameters.ToSplitTrimString();
            this.delurl.Text = cTab.DeleteUrl;
            this.delurlparam.Text = cTab.DeleteUrlParameters.ToSplitTrimString();
            this.showurl.Text = cTab.ShowUrl;
            this.showurlparam.Text = cTab.ShowUrlParameters.ToSplitTrimString();

            //遗留权限控制
            this.allowroles.CheckAll(cTab.AllowRoles);
            this.allowusers.CheckAll(cTab.AllowUsers);

            

            List<ConfigColumns> columns = cTab.Columns;            
            this.GridView2.DataSource = columns.OrderBy(x=>x.ColumnName);
            this.GridView2.DataBind();

        }

        //字段绑定
        this.GridView1.DataSource = DBFactory.GetSchema().GetColumnsInTable(tableName);
        this.GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string tableName = Request.QueryString["tab"];
        string xmlFileName = Request.QueryString["xml"];

        string xmlFilePath = Server.MapPath("~/configs/tables/" + tableName + "/" + xmlFileName + ".xml");

        ConfigTable cTab = null;
        if (File.Exists(xmlFilePath))
        {
            cTab = xmlFilePath.XmlToObj<ConfigTable>();
            cTab.XmlName = xmlFileName;
        }
        else
        {
            cTab = new ConfigTable(tableName, xmlFileName);            
        }

        cTab.TableShowName = this.tableShowName.Text;
        cTab.LocationText = this.locationText.Text;

        cTab.BeforeInsertClassName = this.beforeinsert.Text;
        cTab.BeforeUpdateClassName = this.beforeupdate.Text;
        cTab.BeforeDeleteClassName = this.beforedelete.Text;

        cTab.AfterInsertClassName = this.afterinsert.Text;
        cTab.AfterUpdateClassName = this.afterupdate.Text;
        cTab.AfterDeleteClassName = this.afterdelete.Text;

        cTab.ParentColumnName = this.pname.Text;
        cTab.ParentRootValue = this.prootvalue.Text;
        int depth_int = 0;
        int.TryParse(this.depth.Text, out depth_int);
        cTab.ChildDepth = depth_int;

        cTab.AllowAdd = this.allowadd.Checked;
        cTab.AllowEdit = this.allowedit.Checked;
        cTab.AllowDel = this.allowdel.Checked;
        cTab.AllowShow = this.allowshow.Checked;
        cTab.SingleType = this.issingle.Checked;
        cTab.AllowMultiSelect = this.allowmselect.Checked;
        cTab.AllowMove = this.allowmove.Checked;
        cTab.TypeTableParamName = this.typetabparamname.Text;
        cTab.AllowOutput = this.allowoutput.Checked;

        cTab.AllowPageing = this.allowpageing.Checked;
        int psize = 10;
        int.TryParse(this.pagesize.Text, out psize);
        cTab.PageSize = psize;

        cTab.PkFiledName = this.pkfieldname.Text;
        cTab.FkFiledName = this.fkfieldname.Text;
        cTab.FkMainTableName = this.fkmaintablename.Text;

        cTab.OrderFieldName = this.orderfieldname.Text;
        cTab.OrderSql = this.ordersql.Text;
        cTab.OrderGroupColumnName = this.ordergroup.Text;

        cTab.AllowRolebrowseContrl = this.allowrolectrl.Checked;
        cTab.AllowUserBrowseContrl = this.allowuserctrl.Checked;

        cTab.ListUrl = this.listurl.Text;
        cTab.ListUrlParameters = this.listurlparam.Text.TrimSplit();
        cTab.ListViewName = this.viewname.Text;
        cTab.ListViewXmlName = this.vxmlname.Text;
        cTab.AddUrl = this.addurl.Text;
        cTab.AddUrlParameters = this.addurlparam.Text.TrimSplit();
        cTab.EditUrl = this.editurl.Text;
        cTab.EditUrlParameters = this.editurlparam.Text.TrimSplit();
        cTab.DeleteUrl = this.delurl.Text;
        cTab.DeleteUrlParameters = this.delurlparam.Text.TrimSplit();
        cTab.ShowUrl = this.showurl.Text;
        cTab.ShowUrlParameters = this.showurlparam.Text.TrimSplit();

        //遗留权限
        cTab.AllowRoles = this.allowroles.GetCheckedValues().TrimSplit();
        cTab.AllowUsers = this.allowusers.GetCheckedValues().TrimSplit();

        //if (!string.IsNullOrEmpty(cTab.FkMainTableName))
        //{
        //    if (!HasTypeTab(cTab.ListUrl))
        //    {
        //        cTab.ListUrl += GetParam(cTab.ListUrl) + "typeTab=" + cTab.FkMainTableName;
        //    }
        //    if (!HasTypeTab(cTab.AddUrl))
        //    {
        //        cTab.AddUrl += GetParam(cTab.AddUrl) + "typeTab=" + cTab.FkMainTableName;
        //    }
        //    if (!HasTypeTab(cTab.EditUrl))
        //    {
        //        cTab.EditUrl += GetParam(cTab.EditUrl) + "typeTab=" + cTab.FkMainTableName;
        //    }
        //    if (!HasTypeTab(cTab.DeleteUrl))
        //    {
        //        cTab.DeleteUrl += GetParam(cTab.DeleteUrl) + "typeTab=" + cTab.FkMainTableName;
        //    }
        //    if (!HasTypeTab(cTab.ShowUrl))
        //    {
        //        cTab.ShowUrl += GetParam(cTab.ShowUrl) + "typeTab=" + cTab.FkMainTableName;
        //    }
        //}

        cTab.ObjToXml(xmlFilePath);
    }

    private string GetParam(string url)
    {
        return url.IndexOf("?") > -1 ? "&" : "?";
    }
    private bool HasTypeTab(string url)
    {
        if (url.ToLower().IndexOf("typetab=") > -1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        

        string tableName = Request.QueryString["tab"];
        tabname = tableName;
        string xmlFileName = Request.QueryString["xml"];
        xmlname = xmlFileName;

        string xmlFilePath = Server.MapPath("~/configs/tables/" + tableName + "/" + xmlFileName + ".xml");

        ConfigTable cTab = xmlFilePath.XmlToObj<ConfigTable>();

        foreach (DataRowView row in DBFactory.GetSchema().GetColumnsInTable(tableName))
        {

            if (cTab[row["column_name"].ToString()] == null)
            {
                cTab.Columns.Add(new ConfigColumns(row["column_name"].ToString(), row["data_type"].ToString(), cTab.TableName));
            }
        }

        for (int i = cTab.Columns.Count - 1; i >= 0; i--)
        {
            ConfigColumns col = cTab.Columns[i];
            bool has = false;
            foreach (DataRowView row in DBFactory.GetSchema().GetColumnsInTable(tableName))
            {
                if (col.ColumnName.ToLower() == row["column_name"].ToString())
                {
                    has = true;
                    break;
                }
            }

            if (!has)
            {
                cTab.Columns.Remove(col);
            }
        }
        cTab.ObjToXml(xmlFilePath);

        Response.Redirect(Request.Url.ToString());
    }
}
